/*
 * Email Notify
 * Copyright: (C) 2008 by Antonio Nastasi
 * Email: sifcenter (at) gmail.it
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "editaccount.h"

#include <kdebug.h>

EditAccount::EditAccount( KConfigGroup& generalGroup, QWidget* parent )
  : QWidget( parent )
{
  setupUi(this);
  addServersList();

  connect( sslCheck_,      SIGNAL( stateChanged( int ) ),
           this,           SLOT(   sslStateChanged()   ) );
  connect( protocolCombo_, SIGNAL( currentIndexChanged( QString ) ),
           this,           SLOT(   slotProtocolIndexChanged( QString ) ) );
  connect( serversCombo_,  SIGNAL( currentIndexChanged ( int ) ),
           this,           SLOT (  slotNameServerChanged( int ) ) );

  generalGroup_ = generalGroup;
}



EditAccount::~EditAccount()
{
  qDeleteAll( serversList_ );
  kDebug() << "DESTROYED";
}



void EditAccount::addServersList()
{
  // The server List
  serverInfo *server = new serverInfo;
  
  // Gmail informations
  server->name = "Gmail";
  server->hostPop3 = "pop.gmail.com";
  server->hostImap = "imap.gmail.com";
  server->isSsl = true;
  serversList_.append( server );
  serversCombo_->addItem( server->name );
  
  // Yahoo informations
  server = new serverInfo;
  server->name = "Yahoo";
  server->hostPop3 = "pop.mail.yahoo.com";
  server->hostImap = "";
  server->isSsl = false;
  serversList_.append( server );
  serversCombo_->addItem( server->name );
  
  // Hotmail informations
  server = new serverInfo;
  server->name = "Hotmail";
  server->hostPop3 = "pop3.live.com";
  server->hostImap = "";
  server->isSsl = true;
  serversList_.append( server );
  serversCombo_->addItem( server->name );

  serversCombo_->addItem( "Other" );
}



void EditAccount::slotNameServerChanged( int index )
{
  if( index == serversList_.count() )
  {
    //if the current item is "Other" set enable the fields
    serverEdit_->setEnabled( true );
    portSpin_->setEnabled( true );
    protocolCombo_->setEnabled( true );
    sslCheck_->setEnabled( true );
    return;
  }

  // Set the values of the fields
  serverInfo *server = serversList_.value( index );

  sslCheck_->setChecked( server->isSsl );
  protocolCombo_->setCurrentIndex( 1 );
  serverEdit_->setText( server->hostPop3 );
  slotProtocolIndexChanged( "POP3" );

  // Disable the fields
  protocolCombo_->setEnabled( server->hostImap.isEmpty() ? false : true );
  serverEdit_->setEnabled( false );
  portSpin_->setEnabled( false );
  sslCheck_->setEnabled( false );
}



void EditAccount::sslStateChanged()
{
  // Adjust port
  slotProtocolIndexChanged( protocolCombo_->currentText() );
}



void EditAccount::slotProtocolIndexChanged( const QString text )
{
  int port = 0;
  if( text == "IMAP" )
  {
    port = ( sslCheck_->checkState() == Qt::Checked ) ? 993 : 143;
    if( serversCombo_->currentText() !=  "Other" )
    {
      serverInfo *server = serversList_.value( serversCombo_->currentIndex() );
      serverEdit_->setText( server->hostImap );
    }
  }
  else
  {
    port = ( sslCheck_->checkState() == Qt::Checked ) ? 995 : 110;
    if( serversCombo_->currentText() !=  "Other" )
    {
      serverInfo *server = serversList_.value( serversCombo_->currentIndex() );
      serverEdit_->setText( server->hostPop3 );
    }
  }

  portSpin_->setValue( port );
}



void EditAccount::loadConfig()
{
  bool check;
  int found = protocolCombo_->findText( generalGroup_.readEntry( "Protocol", "POP3" ) );
  protocolCombo_->setCurrentIndex( ( found == -1 ) ? 1 : found );

  serverEdit_->setText( generalGroup_.readEntry( "Server", "" ) );
  portSpin_->setValue( generalGroup_.readEntry( "Port", 110 ) );

  check = generalGroup_.readEntry( "SSL", false );
  sslCheck_->setCheckState( ( check ) ? Qt::Checked : Qt::Unchecked );

  intervalSpin_->setValue( generalGroup_.readEntry( "Interval", 60 ) );
  usernameEdit_->setText( generalGroup_.readEntry( "Login", "" ) );
  passwordEdit_->setText( QByteArray::fromBase64( generalGroup_.readEntry( "Password", "" ).toUtf8() ) );

  QString server = generalGroup_.readEntry( "ServerName", "" );

  // Set current name server and adjust the fields
  int founded = serversCombo_->findText( server );
  serversCombo_->setCurrentIndex( ( founded != -1 ) ? founded : 3 );
}



void EditAccount::saveConfig()
{
  generalGroup_.writeEntry( "ServerName", serversCombo_->currentText() );
  generalGroup_.writeEntry( "Server", serverEdit_->text() );
  generalGroup_.writeEntry( "Port", portSpin_->value() );
  generalGroup_.writeEntry( "Protocol", protocolCombo_->itemText(protocolCombo_->currentIndex()));
  if(sslCheck_->checkState() == Qt::Checked)
    generalGroup_.writeEntry( "SSL", "true" );
  else
    generalGroup_.writeEntry( "SSL", "false" );
  generalGroup_.writeEntry( "Interval",  intervalSpin_->value() );
  generalGroup_.writeEntry( "Login", usernameEdit_->text() );
  generalGroup_.writeEntry( "Password",  passwordEdit_->text().toUtf8().toBase64() );
}

#include "editaccount.moc"
